package com.example.milimili;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.milimili.bean.NewsInfo;

import java.util.List;

public class CollectActivity extends AppCompatActivity {
    private List<NewsInfo> newsInfoList;
    private ListView listView;
    private TextView newsText;
    private ImageView myImage;
    private ImageButton backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        MySQLiteOpenHelper mySQLiteOpenHelper=new MySQLiteOpenHelper(this);
        newsInfoList=mySQLiteOpenHelper.queryAllInfo(1);
        initView();
    }

    private void initView() {
        listView=findViewById(R.id.listViewCollect);
        MyAdapter myAdapter=new MyAdapter();
        listView.setAdapter(myAdapter);
        backBtn=findViewById(R.id.backButton);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(CollectActivity.this,MainActivity.class);
                CollectActivity.this.startActivity(intent);
            }
        });
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return newsInfoList.size();
        }

        @Override
        public Object getItem(int i) {
            String str=newsInfoList.get(i).getTitle();
            return str;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View itemView=View.inflate(CollectActivity.this,R.layout.itemlayout,null);
            newsText=itemView.findViewById(R.id.name);
            myImage=itemView.findViewById(R.id.picture);
            newsText.setText(newsInfoList.get(i).getTitle());
            String imageAll=newsInfoList.get(i).getImage();
            String[] imageArray=imageAll.split(", ");
            Glide.with(CollectActivity.this).load(imageArray[0]).into(myImage);
            return itemView;
        }
    }

}